#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Created: Tue May 18 19:13:13 2010

from sys import argv
from PyQt4 import QtCore, QtGui

class FrameNetworkConfig(QtGui.QFrame):
    def __init__(self, connection, parent = None):
        super(FrameNetworkConfig, self).__init__(parent)
        """setup Interface"""
        self.setupUi(self)
        
        self.connection = connection
        self.networkConfig = self.connection.root.Modules.NetworkConfig.NetworkConfig()
        interfacesList = self.networkConfig.getInterfaces()
        for interface in interfacesList:
            self.combo_selectInterface.addItem(interface)

    def setupUi(self, Network_config_frame):
        Network_config_frame.setObjectName("Network_config_frame")
        Network_config_frame.resize(863, 580)
        Network_config_frame.setGeometry(QtCore.QRect(210, 45, 831, 581))
        Network_config_frame.setFrameShape(QtGui.QFrame.StyledPanel)
        Network_config_frame.setFrameShadow(QtGui.QFrame.Raised)
        self.gridLayout_5 = QtGui.QGridLayout(Network_config_frame)
        self.gridLayout_5.setObjectName("gridLayout_5")
        self.gridLayout_2 = QtGui.QGridLayout()
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.btn_showNetworkInfo = QtGui.QPushButton(Network_config_frame)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("icons/edit-find.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_showNetworkInfo.setIcon(icon)
        self.btn_showNetworkInfo.setObjectName("btn_showNetworkInfo")
        self.gridLayout_2.addWidget(self.btn_showNetworkInfo, 0, 0, 1, 1)
        self.plain_networkInfo = QtGui.QTextEdit(Network_config_frame)
        self.plain_networkInfo.setObjectName("plain_networkInfo")
        self.plain_networkInfo.setFixedSize(500, 580)
        self.gridLayout_2.addWidget(self.plain_networkInfo, 1, 0, 1, 1)
        self.gridLayout_5.addLayout(self.gridLayout_2, 0, 0, 4, 1)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label_9 = QtGui.QLabel(Network_config_frame)
        self.label_9.setObjectName("label_9")
        self.horizontalLayout.addWidget(self.label_9)
        self.combo_selectInterface = QtGui.QComboBox(Network_config_frame)
        self.combo_selectInterface.setObjectName("combo_selectInterface")
        self.combo_selectInterface.addItem("")
        self.horizontalLayout.addWidget(self.combo_selectInterface)
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.gridLayout_5.addLayout(self.horizontalLayout, 0, 1, 1, 1)
        self.groupBox = QtGui.QGroupBox(Network_config_frame)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout_4 = QtGui.QGridLayout(self.groupBox)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.label = QtGui.QLabel(self.groupBox)
        self.label.setObjectName("label")
        self.gridLayout_4.addWidget(self.label, 0, 0, 1, 1)
        self.text_IPAddress = QtGui.QLineEdit(self.groupBox)
        self.text_IPAddress.setObjectName("text_IPAddress")
        self.gridLayout_4.addWidget(self.text_IPAddress, 0, 1, 1, 1)
        self.label_8 = QtGui.QLabel(self.groupBox)
        self.label_8.setObjectName("label_8")
        self.gridLayout_4.addWidget(self.label_8, 1, 0, 1, 1)
        self.text_Netmask = QtGui.QLineEdit(self.groupBox)
        self.text_Netmask.setObjectName("text_Netmask")
        self.gridLayout_4.addWidget(self.text_Netmask, 1, 1, 1, 1)
        self.btn_SetIP = QtGui.QPushButton(self.groupBox)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("icons/preferences-other.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_SetIP.setIcon(icon1)
        self.btn_SetIP.setObjectName("btn_SetIP")
        self.gridLayout_4.addWidget(self.btn_SetIP, 2, 1, 1, 1)
        self.gridLayout_5.addWidget(self.groupBox, 1, 1, 1, 1)
        self.groupBox_2 = QtGui.QGroupBox(Network_config_frame)
        self.groupBox_2.setObjectName("groupBox_2")
        self.gridLayout_3 = QtGui.QGridLayout(self.groupBox_2)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.label_10 = QtGui.QLabel(self.groupBox_2)
        self.label_10.setObjectName("label_10")
        self.gridLayout_3.addWidget(self.label_10, 0, 0, 1, 1)
        self.text_Gateway = QtGui.QLineEdit(self.groupBox_2)
        self.text_Gateway.setObjectName("text_Gateway")
        self.gridLayout_3.addWidget(self.text_Gateway, 0, 1, 1, 1)
        self.btn_setGateway = QtGui.QPushButton(self.groupBox_2)
        self.btn_setGateway.setIcon(icon1)
        self.btn_setGateway.setObjectName("btn_setGateway")
        self.gridLayout_3.addWidget(self.btn_setGateway, 1, 1, 1, 1)
        self.gridLayout_5.addWidget(self.groupBox_2, 2, 1, 1, 1)
        self.groupBox_8 = QtGui.QGroupBox(Network_config_frame)
        self.groupBox_8.setObjectName("groupBox_8")
        self.gridLayout = QtGui.QGridLayout(self.groupBox_8)
        self.gridLayout.setObjectName("gridLayout")
        self.label_11 = QtGui.QLabel(self.groupBox_8)
        self.label_11.setObjectName("label_11")
        self.gridLayout.addWidget(self.label_11, 0, 0, 1, 1)
        self.text_PDNS = QtGui.QLineEdit(self.groupBox_8)
        self.text_PDNS.setObjectName("text_PDNS")
        self.gridLayout.addWidget(self.text_PDNS, 0, 1, 1, 1)
        self.label_12 = QtGui.QLabel(self.groupBox_8)
        self.label_12.setObjectName("label_12")
        self.gridLayout.addWidget(self.label_12, 1, 0, 1, 1)
        self.text_SDNS = QtGui.QLineEdit(self.groupBox_8)
        self.text_SDNS.setObjectName("text_SDNS")
        self.gridLayout.addWidget(self.text_SDNS, 1, 1, 1, 1)
        self.btn_setDNSs = QtGui.QPushButton(self.groupBox_8)
        self.btn_setDNSs.setIcon(icon1)
        self.btn_setDNSs.setObjectName("btn_setDNSs")
        self.gridLayout.addWidget(self.btn_setDNSs, 2, 1, 1, 1)
        self.gridLayout_5.addWidget(self.groupBox_8, 3, 1, 1, 1)
        
        
        ##########################   Signals Connections   ############################
        
        self.connect(self.btn_showNetworkInfo, QtCore.SIGNAL('clicked()'), self.info)
        self.connect(self.btn_SetIP, QtCore.SIGNAL('clicked()'), self.setIP)
        self.connect(self.btn_setGateway, QtCore.SIGNAL('clicked()'), self.setGateway)
        self.connect(self.btn_setDNSs, QtCore.SIGNAL('clicked()'), self.setDNSAddr)
        QtCore.QObject.connect(self.combo_selectInterface, QtCore.SIGNAL("currentIndexChanged(QString)"), self.showInterfaceInfo)
        ###############################################################################
        
        self.retranslateUi(Network_config_frame)
        QtCore.QMetaObject.connectSlotsByName(Network_config_frame)

    def retranslateUi(self, Network_config_frame):
        Network_config_frame.setWindowTitle(QtGui.QApplication.translate("Network_config_frame", "Frame", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_showNetworkInfo.setText(QtGui.QApplication.translate("Network_config_frame", "List current Network Info", None, QtGui.QApplication.UnicodeUTF8))
        self.label_9.setText(QtGui.QApplication.translate("Network_config_frame", "Interface", None, QtGui.QApplication.UnicodeUTF8))
        self.combo_selectInterface.setItemText(0, QtGui.QApplication.translate("Network_config_frame", "select interface...", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox.setTitle(QtGui.QApplication.translate("Network_config_frame", "Set IP/Netmask", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Network_config_frame", "IP Address", None, QtGui.QApplication.UnicodeUTF8))
        self.label_8.setText(QtGui.QApplication.translate("Network_config_frame", "Netmask", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_SetIP.setText(QtGui.QApplication.translate("Network_config_frame", "Set IP", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_2.setTitle(QtGui.QApplication.translate("Network_config_frame", "Set Gateway", None, QtGui.QApplication.UnicodeUTF8))
        self.label_10.setText(QtGui.QApplication.translate("Network_config_frame", "Gateway", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_setGateway.setText(QtGui.QApplication.translate("Network_config_frame", "Set Gateway", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_8.setTitle(QtGui.QApplication.translate("Network_config_frame", "Set domain name servers", None, QtGui.QApplication.UnicodeUTF8))
        self.label_11.setText(QtGui.QApplication.translate("Network_config_frame", "Primary DNS", None, QtGui.QApplication.UnicodeUTF8))
        self.label_12.setText(QtGui.QApplication.translate("Network_config_frame", "Secondary DNS", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_setDNSs.setText(QtGui.QApplication.translate("Network_config_frame", "Configure", None, QtGui.QApplication.UnicodeUTF8))
        ########## set tool tips #############
        self.text_IPAddress.setToolTip(QtGui.QApplication.translate("Form", "pass the IP address", None, QtGui.QApplication.UnicodeUTF8))
        self.text_Netmask.setToolTip(QtGui.QApplication.translate("Form", "pass the Net Mask", None, QtGui.QApplication.UnicodeUTF8))
        self.text_Gateway.setToolTip(QtGui.QApplication.translate("Form", "pass the Gateway", None, QtGui.QApplication.UnicodeUTF8))
        self.text_PDNS.setToolTip(QtGui.QApplication.translate("Form", "pass the primary domain name server", None, QtGui.QApplication.UnicodeUTF8))
        self.text_SDNS.setToolTip(QtGui.QApplication.translate("Form", "pass the secondary domain name server", None, QtGui.QApplication.UnicodeUTF8))
        
    
    
    def showInterfaceInfo(self):
        current = str(self.combo_selectInterface.currentText())
        infinfo = self.networkConfig.getIntrInfo(current)
        self.plain_networkInfo.setText(infinfo)

    
    def validIP(self, address):
        parts = address.split(".")
        if len(parts) != 4:
            return False
        for item in parts:
            if not 0 <= int(item) <= 255:
                return False
        return True

    def info(self):
        #interface = str(self.combo_selectInterface.currentText())
        self.plain_networkInfo.setText(self.networkConfig.getInfo())
    
            
    def setIP(self):
        interface = str(self.combo_selectInterface.currentText())
        ipAddr = str(self.text_IPAddress.text())
        netmask = str(self.text_Netmask.text())
        getcurrent = str(self.networkConfig.getIP(interface))
        
        if ipAddr == "":
            if self.validIP(netmask) == True:
                if self.networkConfig.setIP(interface):
                    QtGui.QMessageBox.information(self, "Information","(DHCP) IP address changed successfully \n interface: "+getcurrent)
                else :
                    QtGui.QMessageBox.critical(self, "Exception","Choose correct interface!")
            else:
                QtGui.QMessageBox.critical(self, "Exception","Not valid subnet mask")
        elif netmask != "":
            if (self.validIP(ipAddr) == True) and (self.validIP(netmask) == True):
                if  self.networkConfig.setIP(interface,ipAddr,netmask):
                    QtGui.QMessageBox.information(self, "Information","(Static) IP address changed successfully \n interface: "+getcurrent)
                    self.text_IPAddress.clear()
                    self.text_Netmask.clear()
                else:
                    QtGui.QMessageBox.critical(self, "Exception","IP & Subnet mask must entered correctrly")
            else:
                QtGui.QMessageBox.critical(self, "Exception","Not valid IP address or netmask")
        else:
            QtGui.QMessageBox.critical(self, "Exception","you must enter the subnet mask")
    
    
    def setGateway(self):
        gateway = str(self.text_Gateway.text())
        if self.validIP(gateway) == True:
            if self.networkConfig.setGateway(gateway):
                QtGui.QMessageBox.information(self, "Information","succeeded to change the default gateway "+gateway)
                self.text_Gateway.clear()
            else:
                QtGui.QMessageBox.critical(self, "Exception","failed to change the default gateway")
        else:
            QtGui.QMessageBox.critical(self, "Exception","Not valid gateway")
    
            
    def setDNSAddr(self):
        PDNS = str(self.text_PDNS.text())
        SDNS = str(self.text_SDNS.text())
        if (self.validIP(PDNS) == True) and (self.validIP(SDNS) == True):
            if self.networkConfig.setDNS(PDNS, SDNS):
                QtGui.QMessageBox.information(self, "Information","succeeded to add name servers\n "+PDNS+" , "+SDNS)
                self.text_PDNS.clear()
                self.text_SDNS.clear()
            else:
                QtGui.QMessageBox.critical(self, "Exception","failed to to add name servers")
        else:
            QtGui.QMessageBox.critical(self, "Exception","Not valid domain name servers")
        
if __name__ == '__main__':
    app = QtGui.QApplication(argv)
    abstract_window = FrameNetworkConfig()
    abstract_window.show()
    app.exec_()

